//
//  MSSettingListViewController.m
//  MinShengBank
//
//  Created by chen wei on 13-10-11.
//  Copyright (c) 2013年 Hq. All rights reserved.
//

#import "MSSettingListViewController.h"
#import "MSPswChangeViewController.h"
#import "BSAppDelegate.h"
#import "MSAboutViewcontrollViewController.h"
#import "RequestServer.h"
#import "SQLite.h"
#import "UrlImageView.h"
#import "HILoginViewController.h"
#import "FVPersenViewController.h"

@interface MSSettingListViewController ()<UIActionSheetDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate,RequestServerDelegate>
{
    NSString * serveradder;
    int tag;
}
@property(nonatomic, retain)UITableView *tableview;
@property(nonatomic, retain)NSString *datapath;
@end

@implementation MSSettingListViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithHexString:@"f0f0f0"];
    tag = 0;
	self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height) style:UITableViewStyleGrouped];
    self.tableview.backgroundView = nil;
    self.tableview.scrollEnabled = NO;
    self.tableview.dataSource = self;
    self.tableview.delegate = self;
    [self.view addSubview:self.tableview];
    serveradder = [[NSString alloc]init];
    
    self.title = @"个人设置";
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(10, 0, 30, 30);
    [button setImage:[UIImage imageNamed:@"返回.png"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
}

#pragma mark 返回
-(void)goBack
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 45;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 2;
    }
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString* reuseIdentifier = [NSString stringWithFormat:@"section=%d,row=%d", indexPath.section, indexPath.row];
    UITableViewCell *cell;
    cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
    }
    if (indexPath.section == 0) {
        if (indexPath.row == 1) {
            cell.textLabel.text = @"修改密码";
            cell.imageView.image = [UIImage imageNamed:@"pswChange.png"];
        }
        if (indexPath.row == 0) {
                cell.textLabel.text = @"个人信息";
                cell.imageView.image = [UIImage imageNamed:@"个人信息.png"];
            }
    }else{
        if (indexPath.row == 1) {
            cell.textLabel.text = @"关于我们";
            cell.imageView.image = [UIImage imageNamed:@"abuot.png"];
        }if (indexPath.row == 0) {
            cell.textLabel.text = @"检测更新";
            cell.imageView.image = [UIImage imageNamed:@"版本更新.png"];
        }
    }

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.selectionStyle = UITableViewCellSelectionStyleGray;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section == 1) {
        return 75;
    }
    return 5;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if (section == 1) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 30, 320, 75)];
        UIButton *logoutButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [logoutButton setFrame:CGRectMake(10, 35, 300, 40)];
        [logoutButton setTitle:@"退 出" forState:UIControlStateNormal];
        logoutButton.backgroundColor = [UIColor colorWithHexString:@"00b5ed"];
        [logoutButton addTarget:self action:@selector(logout:) forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:logoutButton];
        return view;
    }
    return  nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    BSAppDelegate *appDelegate = (BSAppDelegate*)[[UIApplication sharedApplication] delegate];
   
    if (indexPath.section== 0) {
        if (indexPath.row == 1) {
            MSPswChangeViewController *view = [[MSPswChangeViewController alloc] init];
            [appDelegate.navigationController pushViewController:view  animated:YES];
        }if (indexPath.row == 0) {
            FVPersenViewController *view = [[FVPersenViewController alloc] init];
            [appDelegate.navigationController pushViewController:view  animated:YES];
        }
    }else{
        if (indexPath.row == 0) {
            NSDictionary* infoDict = [[NSBundle mainBundle] infoDictionary];
            NSString* version = [infoDict objectForKey:@"CFBundleVersion"];
            NSDictionary *para = [NSDictionary dictionaryWithObjectsAndKeys:@"ios",@"deviceId",version, @"appVersion",nil];
            
            [[RequestServer instance] doActionAsync:0 withAction:@"LoginAction$upApp" withParameters:para withDelegate:self];
        }else{
            MSAboutViewcontrollViewController *view = [[MSAboutViewcontrollViewController alloc] init];
            [appDelegate.navigationController pushViewController:view animated:YES];
        }
       
    }
}

- (void)requestFinished:(NSNumber *)code withMessage:(NSString *)message withData:(id)data
{
    NSLog(@"%d",[code intValue]);
    if (code == 0) {
        NSUserDefaults *server = [NSUserDefaults standardUserDefaults];
        NSString *serverUrl = [server objectForKey:@"current_ServerUrl"];
        NSString *serverpath = [NSString stringWithFormat:@"%@%@",serverUrl,[data objectForKey:@"clientPath"]];
        NSURL* url = [[NSURL alloc] initWithString:serverpath];
        [[ UIApplication sharedApplication]openURL:url];
        
    }else if ([code intValue]== 1) {
        ShowMessage(message);
    }
}


- (void)logout:(id)sender
{
    BSAppDelegate *appDelegate = (BSAppDelegate*)[[UIApplication sharedApplication] delegate];
    [appDelegate.navigationController popToRootViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
